//
const baseConfig = require('./baseConfig');
const request = require('./request');
//获取分类数据
const getHomeClass = function () {
  return request.$http.call(this,{
    api:"/getHomeClass",
    data:{}
  }).then(res=>{
    console.log(res)
    if (res.data.code == '1') {
      this.setData({
        sysImgsInfo: res.data.body
      });
    } else {
      return Promise.reject('获取图片分类信息失败');
    }
  }).catch(err=>{
    return Promise.reject('获取图片分类信息失败');
  });
};
//获取分类中对应item_id的数据
const getClassItemInfo = function (item_id) {
  //console.log(item_id);
  return request.$http.call(this,{
    api:"/getClassItemInfo",
    data:{
      item_id:item_id
    }
  }).then(res=>{
    console.log(res) 
    //产品详情数据
    this.setData({
      productInfo:res.data.body
    });
    //console.log(this.data.productInfo);
    //
    let tmpImgData = JSON.parse(res.data.body.info.image).images;
    //console.log(tmpImgData);
    //
    this.setData({
      imageData:tmpImgData,
      pro_url:tmpImgData[0].image
    });
    return Promise.resolve(res.data.body) 
  }).catch(err=>{
    return Promise.reject("获取分类产品详情失败");
  })
};
//获取分类中对应short_id的数据
const getClassItemInfoForShortId = function(short_id){
  //console.log(short_id);
  return request.$http.call(this,{
    api:"/getClassItemInfoForShortId",
    data:{
      short_id:short_id
    }
  }).then(res=>{
    console.log(res) 
    //产品详情数据
    this.setData({
      productInfo:res.data.body
    });
    //console.log(this.data.productInfo);
    //
    let tmpImgData = JSON.parse(res.data.body.info.image).images;
    //console.log(tmpImgData);
    //
    this.setData({
      imageData:tmpImgData,
      pro_url:tmpImgData[0].image
    });
    return Promise.resolve(res.data.body) 
  }).catch(err=>{
    return Promise.reject("获取分类产品详情失败");
  })
};
//上传图片
const uploadOne = function(){
  return new Promise((resolve, reject)=>{
    wx.chooseImage({
      success:res => {
        wx.showLoading({
          title: '上传中...',
          mask:true
        });
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: `${baseConfig.Url}/uploadOne`, //
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {},
          success: res=>{
            wx.hideLoading();
            console.log(res);
            const data = res.data
            //console.log(data);
            resolve(data)
          },
          fail:err=>{
            wx.hideLoading();
            reject("图片上传失败");
          }
        })
      },
      fail:err=>{
        wx.hideLoading();
        reject("选择要上传的图片出错");
      }
    });
  });
};
//
module.exports = {
  getHomeClass,
  getClassItemInfo,
  getClassItemInfoForShortId,
  uploadOne
};